package com.yzd.learnjava.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.yzd.learnjava.util.DaemonThreadFactory;
import com.yzd.learnjava.util.Print;

public class DaemonFromFactory implements Runnable{
	public void run() {
		try {
			while(true) {
				TimeUnit.MILLISECONDS.sleep(100);
				Print.print(Thread.currentThread() + ": " + this);
			}
		} catch (Exception e) {
			Print.print("interupted");
		}
	}
	
	public static void main(String[] args) throws Exception {
		ExecutorService exec = Executors.newCachedThreadPool(new DaemonThreadFactory());
		for(int i = 0 ; i < 10; i++) {
			exec.execute(new DaemonFromFactory());
		}
		Print.print("All daemons started");
		TimeUnit.MILLISECONDS.sleep(500);
		
	}
	
}
